/**
 * Copyright(C) 2012 Luvina
 * PasswordValidator.java, 26-05-2012 NguyenVietDung
 */
package net.luvina.manageuser.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.luvina.manageuser.util.Message;
import net.luvina.manageuser.util.StringUtil;

/**
 * PasswordValidator: Thực hiện validate dữ liệu nhập vào cho hạng mục mật khẩu
 *
 * @author NguyenVietDung
 */
public class PasswordValidator implements Validator {

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public void validate(FacesContext context, UIComponent component, Object str) throws ValidatorException {
		FacesMessage message = new FacesMessage();
		String passwordStr = String.valueOf(str);
		String labelField = (String) component.getAttributes().get("label");
		int passworldStrLeng = passwordStr.length();
		if (passworldStrLeng < 5 || passworldStrLeng > 15) {
			StringBuilder msgStrBuilder = new StringBuilder();
			msgStrBuilder.append(" 「").append(labelField).append("」 ").append(Message.getString("ER007_password"));
			message.setSummary(msgStrBuilder.toString());
			message.setDetail(msgStrBuilder.toString());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
		if (!StringUtil.isHalfsizePassword(passwordStr)) {
			StringBuilder msgStrBuilder = new StringBuilder();
			msgStrBuilder.append(" 「").append(labelField).append("」 ").append(Message.getString("ER018_halfSize"));
			message.setSummary(msgStrBuilder.toString());
			message.setDetail(msgStrBuilder.toString());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}

	}

}
